package edu.uta.iconverter.converter;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import edu.uta.iconverter.ui.GUIConverter;

public class Converter {
  public static StringBuffer replace( StringBuffer textFile, String expression ) throws IOException {
          Dictionary dc = new Dictionary();
          
          HashMap <String, String> substitutions = dc.loadDictionaryFromFile("resources/Dictionary.txt");
         
            StringBuffer output = new StringBuffer(textFile.length());
            
            Pattern pattern = Pattern.compile(expression);
            Matcher matcher = pattern.matcher(textFile);
            while (matcher.find()) {
                System.out.println("found !!!!");
                String candidate = matcher.group();
                String substitute = substitutions.get(candidate);
                if (substitute == null)
                    substitute = candidate; // no match, use original
                matcher.appendReplacement(output, Matcher.quoteReplacement(substitute));
            }
            matcher.appendTail(output);
            
            //System.out.println("output -->>"+output);
            // output now contains the text with substituted words
            
            writeToFileFromBuffer(output,"output/Test.cs");
            return output;
            
          
  }
  
  public static Boolean writeToFileFromBuffer(StringBuffer csFileText, String csFileName){
          
          try {
            BufferedWriter out = new BufferedWriter(new FileWriter(csFileName));
            String outText = csFileText.toString();
            out.write(outText);
            out.close();
                 }
                  
          catch (IOException e){
                  e.printStackTrace();
          }

          
          return true;
          
  }
  
  public static StringBuffer convert(String javaFilePath){
          StringBuffer convertedFileStr = new StringBuffer();
          try{
                  File file = new File(javaFilePath);
          BufferedReader reader = new BufferedReader(new FileReader(file));
          StringBuffer text = new StringBuffer(reader.toString().length());
          
          text.append("using System;\nusing System.Collections.Generic;\n");
          text.append("using System.Linq;\nusing System.Text;\n\n");
          String line = "";
          while((line = reader.readLine()) != null)
          {
                  text.append(line);
                  text.append("\r\n");
          }
          reader.close();
                
                // RegEX for String
          convertedFileStr = text;
          Dictionary dc = new Dictionary();
          String[] substitutions = dc.loadDictionaryWordsFromFile("resources/Dictionary.txt");
          
          for( int i = 0; i < substitutions.length; i++ ) {
        	  convertedFileStr = replace(convertedFileStr, substitutions[i]);
          }
                //convertedFileStr = replace(text,"String");
                //convertedFileStr = replace(convertedFileStr,"System.out.println");
          
                System.out.println("output -->>"+convertedFileStr);
                /*
                 * Replace function with different expressions to replace
                 * 
                 * 
                 */
        } catch (Exception e) {
                e.printStackTrace();
        }  
         return convertedFileStr; 
  }
  
  public static void main (String args[]){
          //ReadWriteTextFile rw = new ReadWriteTextFile("/Users/Denesh/My FIles/workspace/Test/src/test/Testing.java");
          try {
                //Schedule a job for the event dispatch thread:
                //creating and showing this application's GUI.
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        //Turn off metal's use of bold fonts
                        UIManager.put("swing.boldMetal", Boolean.FALSE);
                        GUIConverter.createAndShowGUI();
                    }
                });

        } catch (Exception e) {
                e.printStackTrace();
        }
  }
  
  
}

